package cn.yuanqiao.web.controller.archive.appManager;

import cn.yuanqiao.archive.appManager.domain.TenantSysDictData;
import cn.yuanqiao.archive.appManager.service.ITenantSysDictDataService;
import cn.yuanqiao.archive.appManager.service.ITenantSysDictTypeService;
import cn.yuanqiao.common.utils.StringUtils;
import cn.yuanqiao.common.utils.poi.ExcelUtil;
import cn.yuanqiao.common.web.controller.BaseController;
import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.common.web.page.TableDataInfo;
import cn.yuanqiao.common.annotation.Log;
import cn.yuanqiao.common.enums.BusinessType;
import cn.yuanqiao.common.utils.SecurityUtils;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 数据字典信息
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/archive/tenantDict/data")
public class TenantSysDictDataController extends BaseController {
    @Autowired
    private ITenantSysDictDataService dictDataService;

    @Autowired
    private ITenantSysDictTypeService dictTypeService;

    //@PreAuthorize("@ss.hasPermi('system:dict:list')")
    @Log(title = "查询字典列表", businessType = BusinessType.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(TenantSysDictData dictData) {
        startPage();
        List<TenantSysDictData> list = dictDataService.selectDictDataList(dictData);
        return getDataTable(list);
    }

    @Log(title = "业务字典数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TenantSysDictData dictData) {
        List<TenantSysDictData> list = dictDataService.selectDictDataList(dictData);
        ExcelUtil<TenantSysDictData> util = new ExcelUtil<TenantSysDictData>(TenantSysDictData.class);
        util.exportExcel(response, list, "字典数据");
    }

    /**
     * 查询字典数据详细
     */
    //@PreAuthorize("@ss.hasPermi('system:dict:query')")
    @Log(title = "查询字典数据详细", businessType = BusinessType.SELECT)
    @GetMapping(value = "/{dictCode}")
    public AjaxResult getInfo(@PathVariable Long dictCode) {
        return success(dictDataService.selectDictDataById(dictCode));
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @Log(title = "根据字典类型查询字典数据信息", businessType = BusinessType.SELECT)
    @GetMapping(value = "/type/{dictType}")
    public AjaxResult dictType(@PathVariable String dictType) {
        List<TenantSysDictData> data = dictTypeService.selectDictDataByType(dictType);
        if (StringUtils.isNull(data)) {
            data = new ArrayList<TenantSysDictData>();
        }
        return success(data);
    }

    @GetMapping(value = "/type")
    public AjaxResult dictTypenull() {
        List<TenantSysDictData> data = new ArrayList<TenantSysDictData>();
        return success(data);
    }

    @Log(title = "根据字典类型查询字典数据信息", businessType = BusinessType.SELECT)
    @GetMapping(value = "/typeObject/{dictType}")
    public AjaxResult dictTypeObject(@PathVariable() String dictType) {
        if (org.apache.commons.lang3.StringUtils.isEmpty(dictType)) {
            return success();
        }
        JSONObject object = new JSONObject();
        String[] dicts = dictType.split(",");
        for (String dict : dicts) {
            List<TenantSysDictData> data = dictTypeService.selectDictDataByType(dict);
            if (StringUtils.isNull(data)) {
                data = new ArrayList<TenantSysDictData>();
            }
            object.put(dict, data);
        }
        return success(object);
    }

    @GetMapping(value = "/typeObject")
    public AjaxResult dictTypeObjectnull() {
        JSONObject object = new JSONObject();
        return success(object);
    }

    /**
     * 新增字典类型
     */
    //@PreAuthorize("@ss.hasPermi('system:dict:add')")
    @Log(title = "业务字典数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody TenantSysDictData dict) {
        dict.setCreateBy(SecurityUtils.getUsername());
        return toAjax(dictDataService.insertDictData(dict));
    }

    /**
     * 修改保存字典类型
     */
    //@PreAuthorize("@ss.hasPermi('system:dict:edit')")
    @Log(title = "业务字典数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody TenantSysDictData dict) {
        dict.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(dictDataService.updateDictData(dict));
    }

    /**
     * 删除字典类型
     */
    //@PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @Log(title = "业务字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictCodes}")
    public AjaxResult remove(@PathVariable Long[] dictCodes) {
        dictDataService.deleteDictDataByIds(dictCodes);
        return success();
    }
}
